﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TSites.Core.Domain.MenuMgmt;
using TSites.Administrator.Models;
using TSites.MenuMgmt;

namespace TSites.Administrator.Controllers
{
    public class MenuMgmtController : Controller
    {
        IMenuMgmtService MenuService { get; set; }
        IAdminSession UserInfo { get; set; }
        //
        // GET: /MenuManagement/

        public ActionResult Index()
        {
            IList<Menu> listTmp = MenuService.GetList();
            IList<Menu> listMenu = BuildListMenus(listTmp);
            
            MenuMgmtModel model = new MenuMgmtModel();
            model.ListMenu = listMenu;
            return View("Index",model);
        }

        private IList<Menu> BuildListMenus(IList<Menu> listMenu)
        {
            IList<Menu> result = new List<Menu>();
            if (listMenu != null && listMenu.Count > 0)
            {
                int parentMenuId = 0;
                int count = 0;
                Menu menu;
                //Find Menu with level 1
                while (count < listMenu.Count)
                {
                    menu = listMenu.ElementAt<Menu>(count);
                    if (menu.ParentMenuId == parentMenuId)
                    {
                        result.Add(menu);
                        listMenu.RemoveAt(count);
                    }
                    else
                        count++;
                }
                //Find submenus
                foreach (Menu child in result)
                {
                    if (listMenu.Count > 0)
                        FindSubMenus(child, listMenu);
                    else
                        break;
                }
            }
            return result;
        }
        private void FindSubMenus(Menu parentMenu, IList<Menu> listMenu)
        {
            if (listMenu == null || listMenu.Count == 0)
                return;

            int parentMenuId = parentMenu.ID;
            int count = 0;
            Menu menu;
            if (parentMenu.SubMenus == null)
                parentMenu.SubMenus = new List<Menu>();
            while (count < listMenu.Count)
            {
                menu = listMenu.ElementAt<Menu>(count);
                if (menu.ParentMenuId == parentMenuId)
                {
                    parentMenu.SubMenus.Add(menu);
                    listMenu.RemoveAt(count);
                }
                else
                    count++;
            }
            // continue find childs of this's submenus
            if (parentMenu.SubMenus.Count == 0 || listMenu.Count == 0)
                return;

            foreach(Menu item in parentMenu.SubMenus)
            {
                if (listMenu.Count > 0)
                    FindSubMenus(item, listMenu);
                else
                    break;
            }
        }

        public ActionResult Search()
        {
            //MenuMgmtModel model = new MenuMgmtModel();
            return View();
        }

        [HttpPost]    
        public ActionResult Update([Bind()] MenuMgmtModel menu)
        {
            MenuService.Save(menu.CurMenu);
            return RedirectToAction("Index");
        }
    }
}
